package com.example.accountingapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelperIncome extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "income.db";
    private static final int DATABASE_VERSION = 2;

    private static final String CREATE_TABLE_INCOME =
            "CREATE TABLE " + DatabaseContractIncome.IncomeEntry.TABLE_NAME + " (" +
                    DatabaseContractIncome.IncomeEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    DatabaseContractIncome.IncomeEntry.COLUMN_INCOME + " REAL NOT NULL," +
                    DatabaseContractIncome.IncomeEntry.COLUMN_CATEGORY + " TEXT NOT NULL," +
                    DatabaseContractIncome.IncomeEntry.COLUMN_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP," +
                    DatabaseContractIncome.IncomeEntry.COLUMN_DATE + " TEXT NOT NULL" +
                    ");";



    private static final String CREATE_TABLE_TOTAL_INCOME =
            "CREATE TABLE IF NOT EXISTS " + DatabaseContractIncome.TotalIncomeEntry.TABLE_NAME + " (" +
                    DatabaseContractIncome.TotalIncomeEntry._ID + " INTEGER PRIMARY KEY," +
                    DatabaseContractIncome.TotalIncomeEntry.COLUMN_TOTAL_INCOME + " REAL NOT NULL" +
                    ");";

    public DatabaseHelperIncome(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_INCOME);
        db.execSQL(CREATE_TABLE_TOTAL_INCOME);
        db.execSQL("INSERT INTO " + DatabaseContractIncome.TotalIncomeEntry.TABLE_NAME +
                " (" + DatabaseContractIncome.TotalIncomeEntry._ID + ", " +
                DatabaseContractIncome.TotalIncomeEntry.COLUMN_TOTAL_INCOME + ") " +
                "VALUES (1, 0);");

        db.execSQL("INSERT INTO " + DatabaseContractIncome.IncomeEntry.TABLE_NAME +
                " (" + DatabaseContractIncome.IncomeEntry.COLUMN_INCOME + ", " +
                DatabaseContractIncome.IncomeEntry.COLUMN_CATEGORY + ", " +
                DatabaseContractIncome.IncomeEntry.COLUMN_DATE + ") " +
                "VALUES (0, '其他收入', '2021-01-01');");



    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < 2) {
            db.execSQL("ALTER TABLE " + DatabaseContractIncome.IncomeEntry.TABLE_NAME +
                    " ADD COLUMN " + DatabaseContractIncome.IncomeEntry.COLUMN_CATEGORY + " TEXT;");
            db.execSQL(CREATE_TABLE_TOTAL_INCOME);
            db.execSQL("INSERT INTO " + DatabaseContractIncome.TotalIncomeEntry.TABLE_NAME +
                    " (" + DatabaseContractIncome.TotalIncomeEntry._ID + ", " +
                    DatabaseContractIncome.TotalIncomeEntry.COLUMN_TOTAL_INCOME + ") " +
                    "VALUES (1, 0);");
            db.execSQL("ALTER TABLE " + DatabaseContractIncome.IncomeEntry.TABLE_NAME +
                    " ADD COLUMN " + DatabaseContractIncome.IncomeEntry.COLUMN_DATE + " TEXT NOT NULL DEFAULT '2021-01-01';");
        }
    }
}